package com.pmcc.core.web;

import com.pmcc.core.config.security.JwtUser;
import com.pmcc.core.dto.EmployeeDTO;
import com.pmcc.core.entity.security.Employee;
import com.pmcc.core.entity.security.Role;
import com.pmcc.core.repository.EmployeeRepository;
import com.pmcc.core.service.EmployeeService;
import com.pmcc.utils.AntPage;
import com.pmcc.utils.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by syk on 17/6/21.
 */
@RestController()
@RequestMapping("api/v1/employees")
@Api(value = "EmployeeController", description = "用户相关api")
public class EmployeeController {

    @Autowired
    EmployeeRepository employeeRepository;

    @Autowired
    EmployeeService employeeService;

    /**
     * 查询
     *
     * @return
     */
    @ApiOperation(value = "获取用户列表", notes = "")
    @GetMapping()
    public ResultBean list(AntPage antPage, EmployeeDTO model) {
        return employeeService.queryList(antPage, model);
    }


    /**
     * 重置密码
     * @param model
     * @return
     */
    @PostMapping("/resetPwd")
    public ResultBean rePwdEmp(@RequestBody Employee model) {
        return employeeService.rePwdEmp(model);
    }

    /**
     * 新增或修改
     * @param model
     * @return
     */
    @PostMapping("saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody EmployeeDTO model) {
        return employeeService.saveOrUpdate(model);
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @DeleteMapping()
    public ResultBean deleteEmp(@RequestBody Employee model) {
        return employeeService.deleteEmp(model);
    }

    /**
     * 查询用户关联角色id
     * @param userId
     * @return
     */
    @GetMapping("/queryEmployeeRole")
    public ResultBean queryEmployeeRole(String userId) {
        ResultBean resultBean = new ResultBean();
        Employee model = employeeRepository.findOne(userId);
        Set<String> stringSet = model.getRoleList().stream().map(Role::getId).collect(Collectors.toSet());
        resultBean.setRows(new ArrayList(stringSet));
        resultBean.setResultCode(ResultBean.SUCCESS);
        return resultBean;
    }

    /**
     * 保存 用户与角色关系
     * @param userId
     * @param roles
     * @return
     */
    @PostMapping("/{userId}/saveEmployeeRole")
    @ResponseBody
    public ResultBean saveEmployeeRole(@PathVariable("userId") String userId, @RequestBody List<String> roles) {
        return employeeService.saveEmployeeRole(userId, roles);
    }


    /**
     * 修改密码
     *
     * @param map
     * @return
     */
    @PostMapping(value = "modifyPassword")
    public ResultBean resetPassword(@RequestBody Map<String, String> map) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Employee employee = employeeRepository.findOne(jwtUser.getId());
        String password = map.get("password");//获取输入的密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(password, employee.getPassword())) {
            resultBean.setResultCode(ResultBean.FAIL);
            resultBean.setResultDesc("原密码输入不正确!");
            return resultBean;
        }
        String newPassword = map.get("newPassword");//获取新输入的密码
        employee.setPassword(encoder.encode(newPassword));
        employee.setLastPasswordResetDate(new Date());
        employeeRepository.save(employee);
        resultBean.setResultDesc("修改成功!");
        return resultBean;

    }


    /**
     * 主帐户 新增或修改
     * @param model
     * @return
     */
    @PostMapping("saveOrUpdateForMaster")
    public ResultBean saveOrUpdateForMaster(@RequestBody EmployeeDTO model) {
        return employeeService.saveOrUpdateForMaster(model);
    }

    /**
     * 子帐户 新增或修改
     * @param model
     * @return
     */
    @PostMapping("saveOrUpdateForSub")
    public ResultBean saveOrUpdateForSub(@RequestBody EmployeeDTO model) {
        return employeeService.saveOrUpdateForSub(model);
    }

}
